import ShopRankItem_blxf from "./shopRankItem";


const {ccclass, property} = cc._decorator;

/** 子域上货榜 */

@ccclass
export default class ShopRankPanel_blxf extends cc.Component {

    @property({ type: cc.Node, tooltip: 'item' })
    itemNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: 'item盒子' })
    itemBox: cc.Node = null;

    @property({ type: cc.SpriteAtlas, tooltip: '图集' })
    imgAtlas: cc.SpriteAtlas = null;    

    @property({ type: cc.Node, tooltip: '打开授权提示' })
    tipsNode: cc.Node = null;

    @property({ tooltip: '传入参数' })
    _options: any = {};

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.itemNode.opacity = 0;
    }

    start () {
        
    }

    initData_blxf ( options: any ) {
        // console.log('shopRankPanel >>', options);
        this._options = options;

        this.itemBox.destroyAllChildren();
        this.loadList_blxf();
    }

    update (dt) {
        
    }

    loadList_blxf () {
        let { data } = this._options;
        let _len: number = data.length;
        for (let i = 0; i < _len; i++) {
            this.loadItem_blxf( data[i], i );
        }

        this.tipsNode.active = _len == 0;
    }

    loadItem_blxf ( options: any, index ) {
        options.rankNum = index + 1;
        options.imgAtlas = this.imgAtlas;
        let _itemNode: cc.Node = cc.instantiate( this.itemNode );
        _itemNode.position = cc.v3();
        _itemNode.opacity = 255;
        this.itemBox.addChild( _itemNode );
        _itemNode.getComponent(ShopRankItem_blxf).initData_blxf( options );

        // tt
        let { isMe } = options;
        if ( isMe ) return this.loadUserItem_blxf( options );

        let { userInfo } = this._options;
        if ( !userInfo ) return;
        
        // 玩家数据
        let { nickName, avatarUrl } = userInfo;
        avatarUrl = avatarUrl.split('qlogo')[1];
        if ( options.nickname != nickName ) return;
        if ( !options.avatarUrl.includes(avatarUrl) ) return;
        
        this.loadUserItem_blxf( options );
    }

    /** 用户排名信息 */
    loadUserItem_blxf ( options: any ) {
        this.itemNode.opacity = 255;
        this.itemNode.getComponent(ShopRankItem_blxf).initData_blxf( options );
    }

}
